/*
 * This file is part of Space Invaders: Rehash
 * Copyright (C)2007 Vicent Marti (tanoku@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __GAME_H__
#define __GAME_H__

#include "SDL.h"
#include <stdio.h>
#include <string>
#include <vector>

#include "timer.h"
#include "main.h"
#include "sprites.h"
#include "game_player.h"
#include "game_missile.h"
#include "game_alien.h"
#include "game_const.h"

class Game : public SI_Screen
{
// TODO: what a var mess, clean this up...
// at least 3 unused vars around.

	int m_status;
	bool m_finished;
	Player *m_player;
	std::vector<Alien*> m_enemies;
	std::vector<Sprite*> m_sprites;
	
	SimpleTimer m_gamet;
	SimpleTimer m_endgamet;
	SimpleTimer m_bossTimer;
	
	int m_EM_x;
	int m_EM_y;
	bool m_EM_return;
	
	int m_keymap;
	Uint32 m_lastPause;
	Uint32 m_lastPauseKey;
	bool m_canPause;
	float m_pauseGfx;
	
	int m_gameRules;
	bool m_init;
	
	int m_score;
	int m_scoreGfx;
	
	int m_barrierX[MAX_BARRIERS];
	int m_barrierHealth[MAX_BARRIERS];
	int m_barrierCount;
	int m_placingBarrier;
	Uint32 m_lastPlace;
	
	Uint32 m_coundownStart;
	
	int m_level;

	int m_pauseSelect;
	
public:
	Game( int gamerules, int level );
	
	void init();
	
	int getGameRules()
	{
		return m_gameRules;
	}
	
	void generateEnemies();
	
	bool finished() { return m_finished; }
	void tick();
	void onEvent( SDL_Event *event );
	void render();
	void renderStatus();
	void renderSprites();
	
	void startCountdown()
	{
		m_status = STATUS_COUNTDOWN;
		m_coundownStart = SDL_GetTicks();
		m_canPause = false;
	}
	
	int getNextScreen()
	{
		if ( m_status == STATUS_GAMEOVER )
			return SCREEN_MAINMENU;
		
		if ( m_status == STATUS_WIN )
			return SCREEN_GAME_NEWCUTSCENE;
			
		return SCREEN_NONE;
	}
	
	int getBarrierCount()
	{
		return m_barrierCount;
	}
	
	void newSprite( int type, int m_x, int m_y, int size = 0 );
	
	void getEnemyPosition( int row, int col, int &x, int &y );
	int translateKeyPress( int key );
	void translateColor( int color, float &r, float &g, float &b );
	
	Alien* getEnemy( int i )
	{
		if ( i < (int)m_enemies.size() && i >= 0 )
			return m_enemies[i];
		return NULL;
	}

	Player* getPlayer()
	{
		return m_player;
	}
	
	int getStatus() { return m_status; }

	void barrierHit( int barrier, int damage );
	
	bool getBarrierBounds( int barrier, SDL_Rect *r );

	void bossKill()
	{
		m_bossTimer.start();
	}

	bool isBossKilling()
	{
		return ( m_bossTimer.get_ticks() > 0 );
	}
};

#endif

